using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace StickyChat
{
    public class TextBoxWriter : System.IO.TextWriter
    {
        private System.Text.StringBuilder buffer;
        private TextBox myTextBox;

        public TextBoxWriter(TextBox tb)
        {
            buffer = new System.Text.StringBuilder();
            myTextBox = tb;
        }

        public override void Write(char c)
        {
            buffer.Append(c);
            if (c == '\n')
            {
                myTextBox.Text += buffer;
                buffer = new System.Text.StringBuilder();
                myTextBox.SelectionLength = 0;
                myTextBox.SelectionStart = myTextBox.TextLength - 1;
                myTextBox.ScrollToCaret();
            }
        }

        public override System.Text.Encoding Encoding
        {
            get { return System.Text.Encoding.Default; }
        }
    }
}
